#!/bin/bash

# 提示用户输入密码
echo "请输入您的sudo密码: "
read -s password
export SUDO_ASKPASS_PASSWORD="$password"

# 定义一个函数来执行需要 sudo 的命令
sudo_command() {
    echo "$SUDO_ASKPASS_PASSWORD" | sudo -S $1
}

# 逐行执行命令
commands=(
    "zen_nospam --info --text='正在初始化系统环境...' --width=200 --height=100"
    "steamos-readonly disable"
    "echo 'Server = https://steamdeck-packages.steamos.cloud/archlinux-mirror/\$repo/os/\$arch' | sudo tee /etc/pacman.d/mirrorlist > /dev/null"
    "flatpak remote-modify flathub --url=https://mirror.sjtu.edu.cn/flathub"
    "cat <<EOF | sudo tee /etc/pacman.conf > /dev/null
#
[options]
DBPath = /usr/lib/holo/pacmandb/
HoldPkg     = pacman glibc
Architecture = auto
Color
CheckSpace
ParallelDownloads = 10
SigLevel    = Never
LocalFileSigLevel = Optional

[jupiter-3.5]
Include = /etc/pacman.d/mirrorlist

[holo-3.5]
Include = /etc/pacman.d/mirrorlist

[core-3.5]
Include = /etc/pacman.d/mirrorlist

[extra-3.5]
Include = /etc/pacman.d/mirrorlist

[community-3.5]
Include = /etc/pacman.d/mirrorlist

[multilib-3.5]
Include = /etc/pacman.d/mirrorlist

[archlinuxcn]
Server = https://mirrors.aliyun.com/archlinuxcn/\$arch
EOF"
    "rm -rf /etc/pacman.d/gnupg"
    "pacman-key --init"
    "pacman-key --populate"
    "pacman -Syyu --noconfirm"
    "pacman -S archlinux-keyring --noconfirm"
    "pacman -S archlinuxcn-keyring --noconfirm"
    "pacman -Syyu --noconfirm"
    "echo -e '\\e[34m安装必要软件包中...\\e[0m'"
    "sed -i 's%#zh_CN.UTF-8 UTF-8%zh_CN.UTF-8 UTF-8%' /etc/locale.gen"
    "rm -rf ~/.cache/locale"
    "echo 'LANG=zh_CN.UTF-8' | sudo tee /etc/locale.conf > /dev/null"
    "locale-gen"
    "zen_nospam --info --text='初始化完毕!建议立即重启系统' --width=300 --height=100"
    "select_main"
)

for cmd in "${commands[@]}"; do
    echo "执行命令: $cmd"
    if [[ $cmd == *sudo* ]]; then
        sudo_command "$cmd"
    else
        eval "$cmd"
    fi
    if [ $? -ne 0 ]; then
        echo "命令 $cmd 执行失败"
        exit 1
    fi
done

echo "所有命令执行成功！"